from google.appengine.ext import db
from google.appengine.api import users
import datetime
import logging
import Entities
import Contacts



#Entities/Contacts n to n relationship data model
class RelsEntityContactModel(db.Model):
    EntityRef	= db.ReferenceProperty(reference_class=Entities.EntitiesModel,collection_name='EntitiesCol')
    ContactRef 	= db.ReferenceProperty(reference_class=Contacts.ContactsModel,collection_name='ContactsCol')
    JobTitle	= db.StringProperty()
    JobEmail	= db.StringProperty()
    JobPhone	= db.StringProperty()
    JobMobile	= db.StringProperty()



def addRelEntityContact(vEntityID, vContactID, vJobTitle, vJobEmail, vJobPhone, vJobMobile, user):
    """
    Add a new RelEntityContact to the datastore.
  
    Returns  
	The id of the RelEntityContact or None if the add failed.
    """
    logging.info('Start addRelEntityContact request')
    if user is None:
	return
  
    try:
	now = datetime.datetime.now()
	logging.info('INFFFFF addRelEntityContact:%s' %vContactID)
	nRelEntityContact = RelsEntityContactModel(
	    EntityRef	= Entities.getEntity(vEntityID,user),
	    ContactRef	= Contacts.getContact(vContactID,user),
	    JobTitle	= vJobTitle,
	    JobEmail	= vJobEmail,
	    JobPhone	= vJobPhone,
	    JobMobile	= vJobMobile
	)
	nRelEntityContact.put()
	logging.info('End addRelEntityContact successfully')
	return nRelEntityContact.key().id()
    except db.Error:
	logging.error('Error: addRelEntityContact')
	return None



def updateRelEntityContact(RelEntityContactID, vEntityID, vContactID, vJobTitle, vJobEmail, vJobPhone, vJobMobile, user):
    """
    Update an RelEntityContact
    
    Returns the RelEntityContact ID or None if an error occurred
    """
    logging.info('Start updateRelEntityContact relid=%s' %RelEntityContactID)
    logging.info('Start updateRelEntityContact entid=%s' %vEntityID)
    logging.info('Start updateRelEntityContact contid=%s' %vContactID)
    
    if user is None:
      return

    try:
    
	now = datetime.datetime.now()
	currRelEntityContact=RelsEntityContactModel.get_by_id(int(RelEntityContactID))
	currRelEntityContact.EntityRef	= Entities.EntitiesModel.get_by_id(int(vEntityID))
	currRelEntityContact.ContactRef = Contacts.ContactsModel.get_by_id(int(vContactID))
	currRelEntityContact.JobTitle	= vJobTitle
	currRelEntityContact.JobEmail	= vJobEmail
	currRelEntityContact.JobPhone	= vJobPhone
	currRelEntityContact.JobMobile	= vJobMobile
	def transact():
	    currRelEntityContact.put()
	db.run_in_transaction(transact)
    
	logging.info('End updateRelEntityContact successfully')
	return RelEntityContactID
    except db.Error:
	logging.error('Error: upateRelEntityContact')
	return None
    


def deleteRelEntityContact(RelEntityContactID, user):
    """
    Delete an RelEntityContact
    
    Returns  
      1 or 0 denpending on success
    """
    logging.info('Start deleteRelEntityContact ID=: %s' %RelEntityContactID)
    if user is None:
      return 0

    try:      
	def transact():
	    currRelEntityContact = RelsEntityContactModel.get_by_id(int(RelEntityContactID))
	    currRelEntityContact.delete()
	db.run_in_transaction(transact)
	
	logging.info('End deleteRelEntityContact successfully ID=: %s' %RelEntityContactID)
	return 1
    except db.Error:
	logging.error('#Error deleteRelEntityContact ID= %s' %RelEntityContactID)
	return 0



def getRelsEntityContact_ByContact(ContactID, user):
    """ 
    Returns  the RelsEntityContact list of the responsible user
    """
    if user is None:
      return None
    
    try:
	cContact=Contacts.ContactsModel.get_by_id(int(ContactID))
	RelsEntityContact = cContact.ContactsCol
  
	return RelsEntityContact
    except db.Error:
	return None



def getRelsEntityContact_ByEntity(EntityID, user):
    """ 
    Returns  the RelsEntityContact list of the responsible user
    """
    if user is None:
      return None
    
    try:	
	cEntity=Entities.EntitiesModel.get_by_id(int(EntityID))
	RelsEntityContact=cEntity.EntitiesCol
  
	return RelsEntityContact
    except db.Error:
	return None



def getRelEntityContact(RelEntityContactID, user):
    """ 
    Returns the RelEntityContact with the ID = RelEntityContactID
    """
    if user is None:
	return  
    
    try:
	oRelEntityContact=RelsEntityContactModel.get_by_id(int(RelEntityContactID))
	return oRelEntityContact
    except db.Error:
	return None
